package vy.org.app.common;

import java.security.KeyException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.vy.dal.dao.VyAddressDO;
import org.vy.dal.dao.VyMemberDO;
import org.vy.dal.seq.MemberSeqGenerator;

public class VyIdGenerator {
	
	private static SimpleDateFormat DATE_FMT = new SimpleDateFormat("MMddyyyy");
	
	public static String generateId(VyMemberDO memDO, List<VyAddressDO> lstAddr) throws KeyException{
		String id = MemberSeqGenerator.getInstance().getNextSequence();
		if(lstAddr == null || lstAddr.isEmpty())
			throw new KeyException("Address information is missing");
		Map<String, VyAddressDO> map = new HashMap<String, VyAddressDO>();
		for(VyAddressDO addr : lstAddr){
			map.put(addr.getAddressType(), addr);
		}
		
		VyAddressDO addr = map.get(AddressTypeEnum.PRESENT.getName());
		if(addr != null){
			id += "/" + DATE_FMT.format(new Date()) 
			+ "/" + addr.getCountry() 
			+ "/" + (addr.getStateOrRegion()!=null? addr.getStateOrRegion(): "000")
			+ "/" + (addr.getDistrictOrCounty()!=null? addr.getDistrictOrCounty():"000");
			return id;
		}
		addr = map.get(AddressTypeEnum.PERMANENT.getName());
		if(addr != null){
			id += "/" + DATE_FMT.format(new Date()) 
			+ "/" + addr.getCountry() 
			+ "/" + (addr.getStateOrRegion()!=null? addr.getStateOrRegion(): "000")
			+ "/" + (addr.getDistrictOrCounty()!=null? addr.getDistrictOrCounty():"000");
			return id;
		}
		addr = map.get(AddressTypeEnum.OFFICE.getName());
		if(addr != null){
			id += "/" + DATE_FMT.format(new Date()) 
			+ "/" + addr.getCountry() 
			+ "/" + (addr.getStateOrRegion()!=null? addr.getStateOrRegion(): "000")
			+ "/" + (addr.getDistrictOrCounty()!=null? addr.getDistrictOrCounty():"000");
			return id;
		}
		return null;
	}
}
